<!--
 * @Description: ------------ fileDescription -----------
 * @Author: snows_l snows_l@163.com
 * @Date: 2024-08-12 16:58:22
 * @LastEditors: snows_l snows_l@163.com
 * @LastEditTime: 2025-07-28 16:24:05
 * @FilePath: \BLOG\src\components\Search\index.vue
-->
<template>
  <div class="search-wrap" :class="{ 'm-search-wrap': isMobi }" @click="handleClickMadel">
    <div class="close-wrap">
      <div class="title">搜索弹窗</div>
      <img style="width: 30px; height: 30px" class="close-icon" :src="getImgIcon('icon-close.svg')" @click="handleClose" />
    </div>
    <div class="modal-wrap-bg">
      <div class="search-content-wrap" :class="{ 'm-search-content-wrap': isMobi }">
        <div class="input-wrap">
          <div class="left-info"></div>
          <input
            class="input"
            :value="state.keyword"
            type="text"
            placeholder="搜索"
            @keydown="e => handleKeyDown(e)"
            @input="e => (state.keyword = e.target.value)"
            @blur="e => handleSearch(e)" />
          <div class="other-wrap pointer">
            <i class="iconfont icon-sousuo1"></i>
          </div>
        </div>
        <div class="serch-content">
          <div class="serch-content-t">
            <div class="have-content-wrap" v-if="state.musicList.length > 0 || state.articleList.length > 0">
              <div class="content-wrap">
                <div class="music-wrap" v-if="state.musicList.length > 0">
                  <div class="music-title">音乐：{{ state.musicList.length }} 条</div>
                  <div class="music-list">
                    <div
                      @click="handlePlay(item)"
                      class="music-item pointer kbn-music intersection-observer hover-shadow"
                      :data-tip="item.title"
                      v-for="(item, index) in state.musicList"
                      :key="index">
                      <div class="music-item-wrap">
                        <div class="cover-wrap" @click="e => e.stopPropagation()">
                          <LImg style="transition: all 0.4s ease-in-out" class="cover-img" :src="item.img" alt="" />
                        </div>
                        <div class="music-info">
                          <div class="info-item text music-title">{{ item.title }}</div>
                          <div class="info-item text music-artist">歌手：{{ item.artist }}</div>
                          <div class="info-item text music-type">类型：{{ state.playList.find(row => row.value == item.type)?.label }}</div>
                        </div>
                      </div>
                      <div class="is-playing-current">
                        <span class="current-music" v-show="state.currentMusicId == item.id">☑️</span>
                        <img v-show="state.isPlaying && state.currentMusicId == item.id" :src="getOnlineImgUrl('playing', '/imgs/common', 'gif')" alt="" />
                      </div>
                    </div>
                  </div>
                </div>
                <div class="article-wrap" v-if="state.articleList.length > 0">
                  <div class="music-title">文章：{{ state.articleList.length }} 条</div>
                  <div class="article-content-wrap">
                    <div
                      class="article-item kbn-read pointer intersection-observer hover-shadow"
                      :data-tip="item.title.length > 16 ? item.title.slice(0, 16) + '...' : item.title"
                      v-for="(item, index) in state.articleList"
                      :key="index">
                      <div class="img-left item-wrap">
                        <div class="item-content">
                          <div class="create-time">
                            <i class="iconfont icon-shijian"></i>
                            <span>发布于：</span>
                            <span>{{ item.createTime }}</span>
                            <div class="type">{{ state.articleTypeList.find(v => v.value == item.type)?.label || '未知类型' }}</div>
                          </div>
                          <div class="article-title" @click="handleArticle(item)">
                            {{ item.title }}
                          </div>
                          <div class="auth-info-wrap">
                            <div>
                              <img :src="state.avatar" vertical-align="middle" alt="" />
                              <span>snows_l</span>
                            </div>
                            <div>
                              <IconHot class="iconfont"></IconHot>
                              <span>{{ item.readCount || 0 }}</span>
                            </div>
                            <div>
                              <IconLike :size="18" class="iconfont" />
                              <span>{{ item.shareCount || 0 }}</span>
                            </div>
                            <div>
                              <IconComment :size="19" class="iconfont"></IconComment>
                              <span>{{ item.commentCount || 0 }}</span>
                            </div>
                            <div class="pointer" data-tip="点击 可直接阅览" v-if="item.isPreview === 1" @click="handleView(item)">
                              <IconPreview class="iconfont" :size="18"></IconPreview>
                              <span class="" style="font-size: 12px">预览</span>
                            </div>
                          </div>
                          <div class="article-des text">
                            {{ item.subTitle }}
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="empry-wrap" v-else>
              <Empty :text="'没有找到相关内容~请重新搜索！'" :loadingText="'搜索内容加载中...'" :loading="state.loading" />
            </div>
          </div>
        </div>
      </div>
      <!-- <div class="xiaoren pointer" @click="handleClose"></div> -->
    </div>
  </div>
</template>

<script lang="ts" setup>
import { live2dReloadElement } from '@/utils/live2d';
import { getArticleList } from '@/api/article';
import { getDict } from '@/api/common';
import { getMusicList } from '@/api/music';
import $bus from '@/bus/index';
import useResize from '@/hooks/useResize';
import { useAppStore } from '@/store/app';
import { getImgIcon, getOnlineImgUrl, getQQAvatar } from '@/utils/common';
import moment from 'moment';
import { onMounted, onUnmounted, onUpdated, reactive, watch } from 'vue';
import { useRouter } from 'vue-router';
const { isMobi } = useResize();

const store = useAppStore();
const router = useRouter();

const props = defineProps({
  show: {
    type: Boolean
  }
});
const emits = defineEmits(['close']);

const state = reactive({
  musicList: [],
  articleList: [],
  playList: [],
  articleTypeList: [],
  keyword: '',
  avatar: getQQAvatar(),
  loading: false,
  currentMusicId: 0,
  isPlaying: localStorage.getItem('isPlaying') == 'true' ? true : false
});

const page = {
  page: 1,
  size: 100
};

const handleKeyDown = (e: any) => {
  if (e.keyCode === 13) {
    handleSearch();
  }
};

// 搜索
const handleSearch = () => {
  let serverUrl = store.projectConfig.bsServerUrl;
  if (state.keyword.trim() === '') return false;
  state.loading = true;
  Promise.all([getMusicList({ title: state.keyword, ...page }), getArticleList({ title: state.keyword, ...page })])
    .then(res => {
      state.musicList = res[0].data.map(item => {
        return {
          ...item,
          img: import.meta.env.VITE_CURRENT_ENV == 'dev' ? import.meta.env.VITE_DEV_BASE_SERVER + item.cover : serverUrl + item.cover
        };
      });
      state.articleList = res[1].data.map(item => {
        return {
          ...item,
          cover: import.meta.env.VITE_CURRENT_ENV == 'dev' ? import.meta.env.VITE_DEV_BASE_SERVER + item.cover : serverUrl + item.cover,
          subTitle: item.subTitle.replace(/&#39;/g, "'"),
          createTime: moment(item.createTime).format('YYYY-MM-DD')
        };
      });
      state.loading = false;
    })
    .catch(() => {
      state.loading = false;
    })
    .finally(() => {
      state.loading = false;
    });
};

// 关闭搜索
const handleClose = () => {
  emits('close');
};

// 获取音乐类型
const getPlayList = () => {
  getDict({ dictType: 'music_type' }).then(res => {
    state.playList = res.data;
    store.SET_MUSIC_DICT(res.data);
  });
};

// 获取文章类型
const getArticleTypeList = () => {
  getDict({ dictType: 'article_type' }).then(res => {
    state.articleTypeList = res.data;
    store.SET_ARTICLE_DICT(res.data);
  });
};

// 播放音乐
const handlePlay = (item: any) => {
  state.currentMusicId = item.id;
  $bus.emit('playMusic', { id: item.id });
};

// 点击空白处关闭搜索 阻止modal事件冒泡
const handleClickMadel = (e: any) => {
  e.stopPropagation();
  e.preventDefault();
};

const handleView = (row: any) => {
  // let prefix = store.projectConfig.blogUrl;
  // window.open(`${prefix}/preview?id=${row.id}`, '_blank');
  router.push({
    path: '/preview',
    query: {
      id: row.id
    }
  });
  emits('close');
};

// 点击文章详情
const handleArticle = (row: any) => {
  router.push({
    path: '/article/detail',
    query: {
      id: row.id
    }
  });
  emits('close');
};

watch(
  () => props.show,
  n => {
    if (n) {
      // 获取音乐类型和文章类型
      store.musicDict.length > 0 ? (state.playList = store.musicDict) : getPlayList();
      store.articleDict.length > 0 ? (state.articleTypeList = store.articleDict) : getArticleTypeList();
      state.loading = false;
      state.isPlaying = localStorage.getItem('isPlaying') == 'true' ? true : false;
      if (!isMobi.value) {
        setTimeout(() => {
          document.querySelector('.search-wrap input')?.focus();
        }, 100);
      }
    }
  }
);

onMounted(() => {
  // 监听 播放器 播放状态变化
  $bus.on('musicPlayerStatusChange', (n: boolean) => {
    state.isPlaying = n;
  });
  // 监听 播放器 当前音乐变化
  $bus.on('musicPlayerCurrentMusicChange', (id: number) => {
    state.currentMusicId = id;
  });
});

onUpdated(() => {
  live2dReloadElement();
});

onUnmounted(() => {
  $bus.off('musicPlayerStatusChange');
  $bus.off('musicPlayerCurrentMusicChange');
});
</script>

<style lang="scss" scoped>
.search-wrap {
  width: 100vw;
  height: 100vh;
  position: relative;
  background-image: url('@/assets/images/bg/bg1.avif');
  .close-wrap {
    position: absolute;
    z-index: 99;
    width: 100%;
    top: 0px;
    right: 0px;
    cursor: pointer;
    background-color: #9eb3f4;
    padding: 5px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .title {
      font-size: 18px;
    }
  }
  .modal-wrap-bg {
    padding: 60px 20px 20px 20px;
    background-color: var(--bg-content-color);
    width: 100%;
    height: 100%;
  }
  .search-content-wrap {
    height: 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100%;
    max-width: var(--content-max-width);
    padding: 60px 0px 30px 0px;
    .input-wrap {
      height: 50px;
      border-radius: var(--border-radius-2);
      border: 1px solid var(--text-color-2);
      overflow: hidden;
      width: 100%;
      background-color: var(--bg-content-color);
      display: flex;
      .input {
        height: 100%;
        width: calc(100% - 80px);
        outline: none;
        border: none;
        font-size: 16px;
        background-color: transparent;
        color: var(--text-color);
      }
      .other-wrap {
        width: 60px;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: transparent;
        i {
          font-size: 24px;
          color: var(--text-color-2);
        }
      }
      .left-info {
        width: 20px;
        height: 100%;
        background-color: transparent;
      }
    }

    .serch-content {
      margin-top: 20px;
      width: 100%;
      height: calc(100% - 80px);
      border-radius: var(--border-radius-2);
      background: var(--bg-content-color);
      overflow: hidden;
      padding: 20px;
      .serch-content-t {
        width: 100%;
        height: 100%;
        overflow: auto;
        .empry-wrap {
          width: 100%;
          height: 100%;
        }
        .have-content-wrap {
          .music-title {
            height: 30px;
            line-height: 30px;
            font-size: 18px;
            color: var(--text-color);
          }
          .music-wrap {
            margin-bottom: 20px;
            .music-list {
              display: flex;
              flex-wrap: wrap;
              justify-content: space-between;
              padding: 5px 5px;
              .music-item {
                width: 390px;
                display: flex;
                align-items: center;
                justify-content: space-between;
                padding: 10px 20px;
                box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
                margin: 10px 0;
                border-radius: var(--border-radius-2);
                background-color: var(--bg-content-color);
                &:hover {
                  .cover-img {
                    transform: scale(1.1);
                  }
                }
                .music-item-wrap {
                  display: flex;
                  align-items: center;
                }
                .is-playing-current {
                  top: 0;
                  right: 20px;

                  display: flex;
                  align-items: center;
                  .current-music {
                    font-size: 18px;
                    margin: 0 10px;
                  }
                  img {
                    width: 30px;
                    height: 30px;
                    border-radius: 50%;
                  }
                }
                .cover-wrap {
                  width: 74px;
                  height: 74px;
                  padding: 3px;
                  .cover-img {
                    width: 100%;
                    height: 100%;
                  }
                }
                .music-info {
                  margin-left: 20px;
                  color: var(--text-color);
                  .music-artist,
                  .music-type {
                    font-size: 12px;
                  }
                  .music-title {
                    margin-bottom: 15px;
                  }
                }
              }
            }
            .m-no-music {
              height: 160px;
            }
          }
          .article-wrap {
            .article-content-wrap {
              padding: 5px 5px;
              max-width: var(--content-max-width);
              margin: 0 auto;
              .article-item {
                margin: 20px 0;
                border-radius: var(--border-radius-2);
                width: 100%;
                height: 100%;
                overflow: hidden;
                border: 1px solid var(--border-color);
                &:hover {
                  .cover-img {
                    transform: scale(1.2);
                  }
                }
                .cover-img-wrap {
                  width: 450px;
                  height: 280px;
                  overflow: hidden;
                  .cover-img {
                    width: 100%;
                    height: 100%;
                    transition: all 0.8s ease-in-out;
                  }
                }
                .item-content {
                  flex: 1;
                  height: 100%;
                  padding: 20px;
                  background-color: var(--bg-content-color);
                  position: relative;

                  .create-time {
                    display: inline-block;
                    height: 20px;
                    font-size: 12px;
                    padding: 4px 8px;
                    background-color: var(--theme-light-color-9);
                    border-radius: 5px;
                    color: var(--theme-color);
                    .iconfont {
                      font-size: 12px;
                      margin-right: 10px;
                    }
                    .type {
                      border: 1px solid var(--theme-light-color-9);
                      border-radius: 5px;
                      padding: 2px 4px;
                      font-size: 12px;
                      position: absolute;
                      top: 20px;
                      right: 20px;
                    }
                  }
                  .article-title {
                    margin-top: 20px;
                    height: 60px;
                    font-size: 20px;
                    line-height: 30px;
                    display: -webkit-box;
                    -webkit-box-orient: vertical;
                    -webkit-line-clamp: 2;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    text-align: left;
                    color: var(--text-color);
                  }
                  .article-title:hover {
                    color: var(--theme-color);
                  }
                  .auth-info-wrap {
                    height: 50px;
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    div {
                      display: flex;
                      align-items: center;
                      .iconfont {
                        margin-right: 10px;
                        font-size: 20px;
                        color: var(--theme-color);
                      }
                      .icon-yanjing {
                        font-size: 24px;
                      }
                      span {
                        font-size: 12px;
                        color: var(--text-color);
                      }
                    }
                    img {
                      width: 30px;
                      height: 30px;
                      border-radius: 50%;
                      margin-right: 10px;
                    }
                  }
                  .article-des {
                    height: 86px;
                    line-height: 21px;
                    display: -webkit-box;
                    -webkit-box-orient: vertical;
                    -webkit-line-clamp: 4;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    color: var(--text-color-2);
                  }
                }
              }
            }
            .m-no-article {
              height: 160px;
            }

            .item-wrap {
              display: flex;
              flex-direction: column;
              height: auto !important;
              .cover-img-wrap {
                width: 100% !important;
                height: 180px !important;
              }

              .item-content {
                .article-title {
                  margin-top: 10px !important;
                  height: 40px !important;
                  line-height: 20px !important;
                  font-size: 16px !important;
                }
              }
              .auth-info-wrap {
                height: 30px !important;
                img {
                  width: 20px !important;
                  height: 20px !important;
                }
              }
              .article-des {
                height: 30px !important;
                line-height: 15px !important;
                font-size: 12px !important;
                -webkit-line-clamp: 2 !important;
              }
            }
          }
        }
      }
    }
  }
  // .xiaoren {
  //   min-width: 284px;
  //   min-height: 388px;
  //   position: absolute;
  //   bottom: 10px;
  //   right: 0;
  //   background-image: url('@/assets/images/common/iloli.gif');
  //   background-size: cover;
  //   background-position: center;
  //   background-repeat: no-repeat;
  // }
}

.m-search-wrap {
  .search-content-wrap {
    padding: 50px 20px 30px 20px;
  }
  .xiaoren {
    min-width: 91px;
    min-height: 120px;
    width: 91px !important;
    height: 120px !important;
  }
}
</style>
